#include "Events.h"
#include <string>
#include"DateList.h"
#include"EnergysEventsList.h"
#include "Event.h"
#include "Poco/DateTime.h"
#include "Poco/DateTimeParser.h"
#include <iostream.h>

using namespace Poco;


using namespace std;
Events::Events()
{
	this->events = vector<EnergysEventsList*>();//delete!!!
	EnergysEventsList* buy = new EnergysEventsList("buy");//delete
	EnergysEventsList* sell = new EnergysEventsList("sell");
	EnergysEventsList* create = new EnergysEventsList("create");
	EnergysEventsList* consume = new EnergysEventsList("consume");
	reports = new DateList("Report");
	events.push_back(buy);
	events.push_back(sell);
	events.push_back(create);
	events.push_back(consume);

}

Events::~Events()
{
	this -> clear();
}

void Events::clear()
{
	//reports -> clear();
	delete (reports);
	reports =0;
	vector<EnergysEventsList*>::iterator it;
	for(it= events.begin();it < events.end();)
	{
		EnergysEventsList* energyList =*it;
	//	energyList ->clear();
		delete energyList;
		energyList = 0;
		events.erase(it);
	}
}

bool Events::noMoreEvents()
{
	bool ans = true;
	vector<EnergysEventsList*>::iterator it;
	for(it = events.begin();it<events.end()&ans; it++)
	{
		EnergysEventsList* j = *it;
		if(!j->noMoreEvents())
		{
			ans = false;
		}
	}
	return (ans&reports->noMoreEvents());
}

void Events::addEvent(Event* e)
{
	vector<EnergysEventsList*>::iterator it;
	bool added = false;
	for(it = events.begin();it<events.end() & !added; it++)
	{
		EnergysEventsList *j = *it;
		if(e->getType()==j->getName())
		{
			//cout<<" i added event the type is "<< e -> getType()<<endl;
			added= true;
			j->addEvent(e);
		}
		if(e->getType().find("Report")<50)
		{
			//cout<<" i added report the type is "<<e -> getType()<<endl;
			added = true;
			reports->addEvent(e);
		}
	}
	if (!added)
	{
		delete (e);
	}
}

Event Events::getBuyingEvent(string name, DateTime date)
{
	vector<EnergysEventsList*>::iterator it;
	for(it = events.begin();it<events.end(); it++)
	{
		EnergysEventsList *j = *it;
		if("buy"==j->getName())
			return (j->getFirst(name,date));
	}
	return Event();
}
Event Events::getSellingEvent(string name,  DateTime date)
{
	vector<EnergysEventsList*>::iterator it;
	for(it = events.begin();it<events.end(); it++)
	{
		EnergysEventsList *j = *it;
		if("sell"==j->getName())
			return (j->getFirst(name,date));
	}
	return Event();
}
Event Events::getConsumingEvent(string name,  DateTime date)
{
	vector<EnergysEventsList*>::iterator it;
	for(it = events.begin();it<events.end(); it++)
	{
		EnergysEventsList *j = *it;
		if("consume"==j->getName())
			return (j->getFirst(name,date));
	}
	return Event();
}
Event Events::getCreatingEvent(string name ,  DateTime date)
{
	vector<EnergysEventsList*>::iterator it;
	for(it = events.begin();it<events.end(); it++)
	{
		EnergysEventsList *j = *it;
		if("create"==j->getName())
			return (j->getFirst(name,date));
	}
	return Event();
}
Event Events::getReportingEvent( DateTime date)
{
	return reports->getFirst(date);
}
DateTime Events::getCorrentDay()
{
	DateTime last = DateTime(9999,12,31);
//	DateTime ans = events.at(0)->getDay();//.getFirstDate();
	vector<EnergysEventsList*>::iterator it;
	for(it = events.begin();it<events.end(); it++)
	{
		EnergysEventsList* j = *it;
		if(j->getDay()<last)
			last = j->getDay();
	}

	if (reports -> getFirstDate() < last)
	{
		last = reports -> getFirstDate();
	}
	return last;
}

void Events::print()
{
	vector<EnergysEventsList*>::iterator it;
	for (it = events.begin();it <  events.end();++it)
	{
		EnergysEventsList* j = *it;
		j -> print();
	}
	reports -> print();

}


